import requests
import json
from notify.base import Base

class Ding(Base):
    """ding notify"""
    def send_message(self, msg):
        url = 'https://oapi.dingtalk.com/robot/send?access_token=d0debc475e5545d17bb8e5dcc8c9d027ff3660d7afecf804251737e063b387f0'
        header = {'Content-Type':'application/json; charset=utf-8'}
        data = {
                "msgtype": "text",
                "text": {
                    "content": msg
                },
                "at": {
                    "atMobiles": [
                    ],
                    "isAtAll": False
                }
            }
        req = requests.post(url,headers=header,data=json.dumps(data))
        if req.status_code == 200 and "ok" in req.text:
            return True
        else:
            return  False

    def send_md(self, title, text):
        url = 'https://oapi.dingtalk.com/robot/send?access_token=d0debc475e5545d17bb8e5dcc8c9d027ff3660d7afecf804251737e063b387f0'
        header = {'Content-Type':'application/json; charset=utf-8'}
        data = {
                 "msgtype": "markdown",
                 "markdown": {
                     "title":title,
                     "text": text
                 },
                "at": {
                    "atMobiles": [
                    ], 
                    "isAtAll": False
                }
             }
        req = requests.post(url,headers=header,data=json.dumps(data))
        if req.status_code == 200 and "ok" in req.text:
            return True
        else:
            return  False